package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.DBHelper;

/**
 * Servlet implementation class SearchJob
 */
@WebServlet("/SearchJob")
public class SearchJob extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	static String JOBID;
	static String COMPANY;
	static String SALARY;
	static String ADDRESS;
	static String COMPANYSIZE;
	static String COMPANYTYPE;
	static String JOBTYPE;
	static String PROVIDER;
	
	static LinkedList<String> jobList = new LinkedList<String>();
	
	static String salaryNO ;
	static String addressNO ;
	static String companysizeNO ;
	static String companytypeNO ;
	static String jobtypeNO ;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchJob() {
        super();
        // TODO Auto-generated constructor stub
    }
    public static boolean getsalary(String salNO,String sal)
    {
    	boolean bool = false;
	    float salary = 0;
		String regex = "\\d*";
//	    Matcher m = Pattern.compile(regex).matcher(sal);
//	    while(m.find()) {
//	        if(!"".equals(m.group()))
//	        	   salary = Float.parseFloat(m.group());
//	    }
	    if (salary==0) {
	    	bool=true;
		}else {
			switch (salNO) {
			case "1":
				bool=true;
				break;
			case "2":
				if(salary<1500)
				{
					bool=true;
				}
				break;
			case "3":
				if(salary>=1500&salary<2000)
				{
					bool=true;
				}
				break;
			case "4":
				if(salary>=2000&salary<3000)
				{
					bool=true;
				}
				break;
			case "5":
				if(salary>=3000&salary<4500)
				{
					bool=true;
				}
				break;
			case "6":
				if(salary>=4500&salary<6000)
				{
					bool=true;
				}
				break;
			case "7":
				if(salary>=6000&salary<8000)
				{
					bool=true;
				}
				break;
			case "8":
				if(salary>=8000&salary<10000)
				{
					bool=true;
				}
				break;
			case "9":
				if(salary>=10000)
				{
					bool=true;
				}
				break;
			default:
				bool= true;
				break;
			}
		}
		return bool;	
    }
    public static boolean getaddress(String addNO,String add)
    {
    	boolean bool=false;
    	switch (addNO) {
		case "1":
			if (add.equalsIgnoreCase("jiangsu")) {
				bool=true;
			}
			break;
		case "2":
			if (add.equalsIgnoreCase("shanghai")) {
				bool=true;
			}
			break;
		case "3":
			if (add.equalsIgnoreCase("zhejiang")) {
				bool=true;
			}
			break;
		case "4":
			if (add.equalsIgnoreCase("anhui")) {
				bool=true;
			}
			break;
		case "5":
			if (add.equalsIgnoreCase("fujian")) {
				bool=true;
			}
			break;
		case "6":
			if (add.equalsIgnoreCase("henan")) {
				bool=true;
			}
			break;
		case "7":
			if (add.equalsIgnoreCase("beijing")) {
				bool=true;
			}
			break;
		case "8":
			if (add.equalsIgnoreCase("hubei")) {
				bool=true;
			}
			break;
		case "9":
			if (!(add.equalsIgnoreCase("jiangsu")|add.equalsIgnoreCase("shanghai")|add.equalsIgnoreCase("zhejiang")
					|add.equalsIgnoreCase("anhui")|add.equalsIgnoreCase("fujian")|add.equalsIgnoreCase("henan")
					|add.equalsIgnoreCase("beijing")|add.equalsIgnoreCase("hubei"))) {
				bool=true;
			}
			break;
		default:
			bool=true;
			break;
		}
		return bool;	
    }
    public static boolean getcompanysize(String comsizeNO,String comsize)
    {
    	boolean bool = false;
	    float companysize = 0;
		String regex = "\\d*";
//	    Matcher m = Pattern.compile(regex).matcher(comsize);
//	    while(m.find()) {
//	        if(!"".equals(m.group()))
//	        	   companysize = Float.parseFloat(m.group());
//	    }
	    if (companysize==0) {
	    	bool=true;
		}else {
			switch (comsizeNO) {
			case "1":
				if(companysize<=50)
				{
					bool=true;
				}
				break;
			case "2":
				if(companysize>=50&companysize<=150)
				{
					bool=true;
				}
				break;
			case "3":
				if(companysize>=150&companysize<=500)
				{
					bool=true;
				}
				break;
			case "4":
				if(companysize>=500)
				{
					bool=true;
				}
				break;
			default:
				bool= true;
				break;
			}
		}
		return bool;	
    }
    public static boolean getcompanytype(String comtypeNO,String comtype)
    {
    	boolean bool=false;
    	switch (comtypeNO) {
		case "1":
			if (comtype.equalsIgnoreCase("Foreign")) {
				bool=true;
			}
			break;
		case "2":
			if (comtype.equalsIgnoreCase("Joint-ventured")) {
				bool=true;
			} 
			break;
		case "3":
			if (comtype.equalsIgnoreCase("SOEs")) {
				bool=true;
			} 
			break;
		case "4":
			
			if (comtype.equalsIgnoreCase("Private company")) {
				bool=true;
			} 
			break;
		default:
			bool=true;
			break;
		}
		return bool;
    }
    
    
    public static boolean getjobtype(String jobtypeNO,String jobtype)
    {
    	boolean bool=false;
    	switch (jobtypeNO) {
		case "1":
			if (jobtype.equalsIgnoreCase("Computer")) {
				bool=true;
			}
			break;
		case "2":
			if (jobtype.equalsIgnoreCase("Financial")) {
				bool=true;
			}
			break;
		case "3":
			if (jobtype.equalsIgnoreCase("Trade")) {
				bool=true;
			}
			break;
		case "4":
			if (jobtype.equalsIgnoreCase("Advertising")) {
				bool=true;
			}
			break;
		case "5":
			if (jobtype.equalsIgnoreCase("Logistics Department")) {
				bool=true;
			}
			break;
		case "6":
			if (jobtype.equalsIgnoreCase("Computer")|jobtype.equalsIgnoreCase("Financial")|jobtype.equalsIgnoreCase("Trade")
					|jobtype.equalsIgnoreCase("Advertising")|jobtype.equalsIgnoreCase("Logistics Department")) {
				bool=true;
			}
			break;
		default:
			bool=true;
			break;
		}
		return bool;
    }
    
    public boolean searchValue(String jobId,String COM,String sa,String add,String comsize,String comtype,String jobtype,String PRO)
    {
    	if (getsalary(salaryNO, sa)&getaddress(addressNO, add)&getcompanysize(companysizeNO, comsize)&getcompanytype(companytypeNO, comtype)&getjobtype(jobtypeNO, jobtype)) {
    		JOBID=jobId;
    		COMPANY=COM;
    		SALARY=sa;
    		ADDRESS=add;
    		COMPANYSIZE=comsize;
    		COMPANYTYPE=comtype;
    		JOBTYPE=jobtype;
    		PROVIDER=PRO;
    	
    		jobList.add(jobId);
    		jobList.add(COM);
    		jobList.add(sa);
    		jobList.add(add);
    		jobList.add(comsize);
	   		jobList.add(comtype);
	   		jobList.add(jobtype);
	   		jobList.add(PRO);
    		
    		
    		return true;
		}else {
			return false;
		}
		
    	
    }
    
    
    
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		salaryNO = request.getParameter("salary");
		addressNO = request.getParameter("address");
		companysizeNO = request.getParameter("companysize");
		companytypeNO = request.getParameter("companytype");
		jobtypeNO = request.getParameter("jobtype");
		
		response.setContentType("text/html");
		response.setHeader("Cache-Control", "no-cache");

	
		Connection con = DBHelper.getConnection();
		Statement stmt=null;
		ResultSet rs=null;
		boolean bol=false;
		PrintWriter out = null;
		
		if (con != null)
		{
			stmt = DBHelper.getStatement(con);
			if (stmt != null)
			{
				try {
					out = response.getWriter();
					rs = DBHelper.executeSelect(stmt,"select * from PROVIDEJOB");
					while (rs.next())
						if (searchValue(rs.getString("JOBID"), rs.getString("COMPANY"), rs.getString("SALARY"), rs.getString("ADDRESS"), rs.getString("COMPANYSIZE"), rs.getString("COMPANYTYPE"), rs.getString("JOBTYPE"), rs.getString("PROVIDER"))) {
							bol=true;
						}
				
					if (bol) {
						out.write(jobList.toString());	
			    		jobList.clear();
					}else {
						out.write("Did not meet the conditions of work!!");
					}	
					
			} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
